<?php

namespace app\backstage\model;

use think\Model;
use think\Validate;
use app\backstage\model\Auth;
class Role extends Model
{   
    protected $name = 'rbac_role';
     //验证数据
     public function valiData($data){
        
        $validate = new Validate([
            'role_name'  => 'require',
//            '__token__' => 'token',
        ]);
            
        
        if (!$validate->check($data)) {
            return ['code'=>101,'msg'=>$validate->getError()];
        }
        
        return ['code'=>200];

    }

    //获取权限列表
    public function authList(){

        $data = Auth::field('id,pid,auth_name')->select();
       
        $topAuth = array();
        $sonAuth = array();
        if($data){
            foreach($data as $v){
                if($v['pid']==0){
                    $topAuth[] = $v;
                }else{
                    $sonAuth[$v['pid']][] = $v;
                }
            }
        }

       return ['topAuth'=>$topAuth,'sonAuth'=>$sonAuth];
    }

    //构建保存权限列表
    public function createAuthList($auth_ids){

        $authData = Auth::where('id','in',$auth_ids)->select();
        
        $auth_list = array();
        if($authData){
           foreach($authData as $k=>$v){
              $auth_list[$k] = $v['auth_controller'].'/'.$v['auth_action']; 
           }
        }
    
        return json_encode($auth_list);

    }
}
